package controllers

import (
	"encoding/json"
	"time"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/services"
)

type EntryreturnController struct {
	BaseController
}

func (c *EntryreturnController) Prepare() {
	//先执行
	c.BaseController.Prepare()
	c.Data["activeSidebarUrl"] = "/entryreturn/flushing"
}

func (c *EntryreturnController) Flushing() {
	c.checkAuthor("entryreturn/flushing")
	if c.Ctx.Input.IsPost() {
		var qs models.ErpFlowingQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &qs)
		qs.DepartmentId = c.curUser.DepartmentId
		qs.Typeid = []interface{}{1}
		qs.IsFlushing = 2
		data, count := models.ErpFlowingPageList(&qs, "Entry__Suppliers")
		c.jsonDataTable(count, data)
	}
	c.display("", "entryreturn/flushing_footerjs.html")
}

func (c *EntryreturnController) Details() {
	c.checkAuthor("entryreturn/flushing")
	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("参数不正确")
	}
	if c.Ctx.Input.IsPost() {
		var qs models.ErpFlowingDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &qs)
		qs.Department = c.curUser.DepartmentId
		qs.FlowingId = id
		data, count := models.ErpFlowingDataPageList(&qs)
		c.jsonDataTable(count, data)
	}
	c.Data["flowing_id"] = id
	c.display("", "entryreturn/details_footerjs.html", "shared/layout_dialog.html")
}

//开始冲红
func (c *EntryreturnController) SaveFlushing() {
	c.checkAuthor("entryreturn/flushing")
	ser := services.EntryreturnService{}
	flowing_id, _ := c.GetInt64("flowing_id")
	if err := ser.SaveFlushing(&c.curUser, flowing_id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}

//冲红查询
func (c *EntryreturnController) QueryFlushing() {
	c.checkAuthor("entryreturn/query_flushing")
	c.Data["activeSidebarUrl"] = "/entryreturn/query_flushing"
	if c.Ctx.Input.IsPost() {
		var qs models.ErpFlowingQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &qs)
		qs.DepartmentId = c.curUser.DepartmentId
		qs.Typeid = []interface{}{2}
		qs.IsFlushing = 2
		data, count := models.ErpFlowingPageList(&qs, "Entry", "Entry__Suppliers")
		c.jsonDataTable(count, data)
	}

	c.display("", "entryreturn/queryflushing_footerjs.html")
}

//冲红明细
func (c *EntryreturnController) FlushingDetails() {
	c.checkAuthor("entryreturn/query_flushing")
	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("参数不正确")
	}
	if c.Ctx.Input.IsPost() {
		var qs models.ErpFlowingDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &qs)
		qs.Department = c.curUser.DepartmentId
		qs.FlowingId = id
		data, count := models.ErpFlowingDataPageList(&qs)
		c.jsonDataTable(count, data)
	}
	c.display("", "entryreturn/flushingdetails_footerjs.html", "shared/layout_dialog.html")
}

//保存退货主表信息
func (c *EntryreturnController) Save() {

	ser := services.EntryreturnService{}
	ser.UpdatedBy = c.curUser.RealName
	ser.CreatedBy = c.curUser.RealName
	ser.UpdatedAt = time.Now().Unix()
	ser.DepartmentId = c.curUser.DepartmentId
	suppliers, _ := c.GetInt64("suppliers")
	ser.Suppliers = suppliers
	id, _ := c.GetInt64("id")
	ser.DeliveryMode, _ = c.GetInt("DeliveryMode")
	ser.PayMethod, _ = c.GetInt("PayMethod")
	ser.Remarks = c.GetString("Remarks")
	if id == 0 {
		ser.CreatedAt = time.Now().Unix()
		ser.Number = services.CreateEntryreturnNumber()
		insertid, err := ser.ErpEntryFormInsert(0)
		if err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		c.jsonResult(enums.JRCodeSucc, "ok", insertid)
	} else {
		if err := ser.ErpEntryFormUpdate(); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		c.jsonResult(enums.JRCodeSucc, "编辑成功", "")
	}
}

//采购退货
func (c *EntryreturnController) Edit() {

	c.checkAuthor("entryreturn/retreat")
	c.Data["activeSidebarUrl"] = "/entryreturn/retreat"
	id, _ := c.GetInt64(":id")

	m := new(models.ErpEntryreturn)

	var err error
	m, err = services.ErpEntryreturnView(id, c.curUser.DepartmentId)
	if err != nil {
		c.pageError(err.Error())
	}
	if m.Auditing != 0 {
		c.pageError("数据不允许编辑")
	}

	if m.DepartmentId != c.curUser.DepartmentId {
		c.pageError("数据不存在")
	}
	c.Data["m"] = m
	c.showErpTypeid()

	_, paymethod, deliverymode := c.erpPublicConfig()
	c.Data["paymethod"] = paymethod
	c.Data["deliverymode"] = deliverymode

	c.display("", "entryreturn/edit_footerjs.html")
}

//选择供应商
func (c *EntryreturnController) SelectSuppliers() {

	_, paymethod, deliverymode := c.erpPublicConfig()
	c.Data["paymethod"] = paymethod
	c.Data["deliverymode"] = deliverymode

	suppliers := models.ErpSuppliersList(&models.ErpSuppliersQueryParam{DepartmentId: c.curUser.DepartmentId})
	c.Data["suppliers"] = suppliers

	c.display("", "entryreturn/selectsuppliers_footerjs.html", "shared/layout_dialog.html")
}

//采购退货
func (c *EntryreturnController) Retreat() {
	c.checkAuthor("entryreturn/retreat")
	c.Data["activeSidebarUrl"] = "/entryreturn/retreat"
	if c.Ctx.Input.IsPost() {
		var qs models.ErpEntryreturnQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &qs)

		qs.DepartmentId = c.curUser.DepartmentId
		data, count := models.ErpEntryreturnPageList(&qs)
		c.jsonDataTable(count, data)
	}
	c.display("", "entryreturn/retreat_footerjs.html")
}

//选择库存
func (c *EntryreturnController) SelectEntry() {
	c.checkAuthor("entryreturn/retreat")
	entry_id, _ := c.GetInt64("entry_id")
	products_id, _ := c.GetInt64("products_id")
	middle_id, _ := c.GetInt64("middle_id")

	if c.Ctx.Input.IsPost() {
		var qs models.ErpInventoryQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &qs)
		qs.Department = c.curUser.DepartmentId
		data, count := models.ErpInventoryPageList(&qs)
		c.jsonDataTable(count, data)
	}
	c.Data["entry_id"] = entry_id
	c.Data["products_id"] = products_id
	c.Data["middle_id"] = middle_id

	//获取产品名称
	if product, err := models.GetErpProductsById(products_id, &c.curUser); err != nil {
		c.pageError("找不到对应的产品信息")
	} else {
		c.Data["product"] = product
	}
	c.display("", "entryreturn/selectentry_footerjs.html", "shared/layout_dialog.html")
}

//保存订单明细
func (c *EntryreturnController) SubmitOrderData() {
	if c.Ctx.Input.IsPost() {
		formPost := []*services.EntryreturnTempFormPost{}
		json.Unmarshal(c.Ctx.Input.RequestBody, &formPost)
		ser := services.EntryreturnService{}
		if middle_id, err := ser.InsertEntryreturnEntryData(formPost); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", middle_id)
		}
	}
	c.jsonResult(enums.JRCodeFailed, "error", "")
}

//保存订单
func (c *EntryreturnController) SubmitOrder() {
	if c.Ctx.Input.IsPost() {
		entry_id, _ := c.GetInt64("entry_id")
		ser := services.EntryreturnService{}
		if err := ser.EntryreturnSubmitOrder(c.curUser.DepartmentId, entry_id); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", "")
		}
	}
	c.jsonResult(enums.JRCodeFailed, "error", "")
}

func (c *EntryreturnController) View() {

	c.checkAuthor("entryreturn/index")

	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("缺少参数")
	}

	entry, err := services.ErpEntryreturnView(id, c.curUser.DepartmentId)
	if err != nil {
		c.pageError("找不到指定的记录")
	}
	c.Data["m"] = entry

	_, paymethod, deliverymode := c.erpPublicConfig()
	c.Data["paymethod"] = paymethod
	c.Data["deliverymode"] = deliverymode
	c.Data["activeSidebarUrl"] = "/entryreturn/retreat"
	c.display("", "entryreturn/view_footerjs.html")

}

//查看明细
func (c *EntryreturnController) EntryreturnData() {
	c.checkAuthor("entryreturn/index")
	entry_id, _ := c.GetInt64("entry_id")
	data := models.ErpEntryreturnDataList(entry_id)
	c.jsonDataTable(0, data)
}

//删除退库
func (c *EntryreturnController) DelRereat() {
	c.checkAuthor("entryreturn/del_rereat")
	id, _ := c.GetInt64("id")
	if id == 0 {
		c.jsonResult(enums.JRCodeFailed, "缺少参数", "")
	}
	err := services.ErpEntryreturnDelete(id)
	if err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}

	c.jsonResult(enums.JRCodeSucc, "删除成功", "")
}

//获取中间表数据
func (c *EntryreturnController) GetMiddleData() {
	entry_id, _ := c.GetInt64("entry_id")
	datas := models.ErpEntryreturnMiddleList(entry_id);
	c.jsonResult(enums.JRCodeSucc, "ok", datas)
}

//按中间表获取明细
func (c *EntryreturnController) GetMiddleDataForEntry() {

	middle_id, _ := c.GetInt64("middle_id")
	ser := services.EntryreturnService{}
	if data, err := ser.GetMiddleDataForEntry(middle_id); err != nil {
		c.jsonResult(enums.JRCodeFailed, "没有数据", "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "ok", data)
	}

}

//删除中间表数据
func (c *EntryreturnController) DeleteMiddleData() {
	middle_id, _ := c.GetInt64("middle_id")
	ser := services.EntryreturnService{}
	if err := ser.DeleteMiddleData(middle_id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}

//确认出库
func (c *EntryreturnController) Confirmout() {
	c.checkAuthor("entryreturn/confirmout")
	if c.Ctx.Input.IsPost() {
		var qs models.ErpEntryreturnQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &qs)
		qs.DepartmentId = c.curUser.DepartmentId
		qs.Auditing = 1
		data, count := models.ErpEntryreturnPageList(&qs)
		c.jsonDataTable(count, data)
	}
	c.Data["activeSidebarUrl"] = "/entryreturn/confirmout"
	c.display("", "entryreturn/confirmout_footerjs.html")
}

//出库
func (c *EntryreturnController) ViewOut() {
	c.checkAuthor("entryreturn/confirmout")

	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("缺少参数")
	}

	entry, err := services.ErpEntryreturnView(id, c.curUser.DepartmentId)
	if err != nil {
		c.pageError("找不到指定的记录")
	}

	//开始确认出库
	if c.Ctx.Input.IsPost() {
		ser := services.EntryreturnService{}
		if err := ser.ConfirmEntryOut(&c.curUser, id); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}

	c.Data["m"] = entry

	_, paymethod, deliverymode := c.erpPublicConfig()
	c.Data["paymethod"] = paymethod
	c.Data["deliverymode"] = deliverymode
	c.Data["activeSidebarUrl"] = "/entryreturn/confirmout"
	c.display("", "entryreturn/viewout_footerjs.html")
}
